package com.thesis.object;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * ListDishes class
 * 
 * save many dishes
 * 
 * @author NgocTuyen
 *
 */
public class ListDishes {
	//instance variable
	private static ListDishes instance = new ListDishes();
	
	//array list save all dishes
	private ArrayList<Dish> listDishes;
	
	/**
	 * Constructor function
	 * 
	 * Constructor
	 * 
	 * @access private 
	 * 
	 * @param null
	 * 
	 * @return null
	 * 
	 */
	private ListDishes(){
		listDishes = new ArrayList<Dish>();
	}
	
	/**
	 * getInstance static function
	 * 
	 * @access public
	 * 
	 * @param null
	 * 
	 * @return ListDishes
	 *  + instance object
	 */
	public static ListDishes getInstance(){
		if (instance == null ){
			instance = new ListDishes();
		}
		return instance;
	}
	
	/**
	 * addDishByJson function
	 * 
	 * add all dishes into list dishes
	 * 
	 * @access public
	 * 
	 * @param listJson: list of all dishes
	 * 
	 * $return void
	 */
	public void addDishByJson(JSONArray listJson){
		for (int i = 0; i < listJson.length(); i++) {
			try {
				addDish((JSONObject)(listJson.get(i)));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * addDish function
	 * 
	 * add a dish in to list dishes
	 * 
	 * @access private
	 * 
	 * @param jsonObj: json of one dish
	 * 
	 * @return void
	 */
	private void addDish(JSONObject jsonObj){
		try {
			Dish dish = new Dish(jsonObj.getInt("_id"), jsonObj.getString("name"), jsonObj.getString("key_search"), jsonObj.getInt("type_id"), jsonObj.getString("type"), jsonObj.getString("material"), jsonObj.getString("cooking"), jsonObj.getString("speciality"), jsonObj.getString("unit"), jsonObj.getInt("cost"), jsonObj.getString("image"));
			listDishes.add(dish);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * getListDishes function
	 * 
	 * get list all dishes in restaurant
	 * 
	 * @access public
	 * 
	 * @param null
	 * 
	 * @return ArrayList<Dish>
	 *  + list of all dishes
	 */
	public ArrayList<Dish> getListDishes(){
		return listDishes;
	}
	public void clear(){
		instance = null;
	}
}
